< previous page page_453 next page >

Page 453
Test it yourself by copying the GetDiskFreeSpace function to the Clipboard. The quickest way to locate a specific function is to enter the first few letters to jump to it in the list.
Before continuing, note a new feature in the API Viewer: the ability to choose Public or Private declarations. All this feature really does is include the word Private or Public for you; you can still edit the API declaration later if desired. Public API declarations can be placed in a code module and accessed from other forms and modules in the project. On the other hand, if you want to place an API declaration in a form, it must be declared as Private. Since our example uses a form, go ahead and click the Private radio button.
Double-click GetDiskFreeSpace, and you will notice that it appears in the Selected Items box. Note that you can double-click additional function names to add them to the Selected Items box. Constants and types used by the API can be displayed if you set the API Type drop-down box, although it is up to you to know which constants go with the selected functions.
When you're finished browsing the list, click the Copy button. This action places any selected items on the Windows Clipboard. Next, start a new Visual Basic Standard EXE project and add a module. Open the Code window for the new module, and choose Paste from the Edit menu. The declaration for GetDiskFreeSpace should appear in your Code window:
Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias
"GetDiskFreeSpaceA" _
                 (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, _
                 lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, _
                 lpTtoalNumberOfClusters As Long) As Long
Now that the declaration is available to your program, you can write code that calls the GetDiskFreeSpace API function. Add a command button to the form, and add code to the Click event to call the GetDiskFreeSpace function, as in the following example:
Private Sub Command1_Click()
   Dim lSecPerClust As Long
   Dim lBytesPerSec As Long
   Dim lFreeClust As Long
   Dim lTotalClust As Long
   Dim lReturn As Long

   lReturn = GetDiskFreeSpace("C:\", lSecPerClust, lBytesPerSec,  _
                 lFreeClust, lTotalClust)

   Msgbox "Free Clusters on drive C = " & lFreeClust

End Sub
The preceding example calls the GetDiskFreeSpace API function with the appropriate parameters and displays one of the returned values in a message box. However, the example is not really practically useful; more than likely, you will be interested in the number of free bytes rather than clusters. Also, if you intend to use this function multiple times within your program, a lot of overhead is required to get back the results. You will solve both of these problems in the next section by creating a wrapper function.

 
< previous page page_453 next page >